//
//  HNSH_LoginViewController.m
//  HNSH_SingleProject
//
//  Created by 李宇廷 on 2023/2/22.
//

#import "HNSH_LoginViewController.h"

//view
#import "HNSH_LoginInputView.h"

//vc
#import "HNSH_RegisterViewController.h"         //注册
#import "HNSH_CodeLoginViewController.h"        //验证码登录
#import "HNSH_SafeHomeViewController.h"         //忘记密码

@interface HNSH_LoginViewController ()

/* 手机号 */
@property (nonatomic, strong) HNSH_LoginInputView *phoneView;
/* 密码 */
@property (nonatomic, strong) HNSH_LoginInputView *pwdView;

/* phoneString */
@property (nonatomic, copy) NSString *phoneString;
/* pwdString */
@property (nonatomic, copy) NSString *pwdString;

@end

@implementation HNSH_LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.phoneString = @"";
    self.pwdString = @"";
    
    [self showNavTitle:@"" showLeftItem:YES];
    self.view.backgroundColor = [UIColor appWhiteLabelColor];
    [self configUI];
}

- (void)configUI {
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(32, kNavBarAndStatusBarHeight+70, 200, 28)];
    titleLabel.text = @"手机号登录";
    titleLabel.textColor = [UIColor appBlack333LabelColor];
    titleLabel.font = [UIFont boldSystemFontOfSize:20];
    [self.view addSubview:titleLabel];
    
    //手机号
    self.phoneView = [[HNSH_LoginInputView alloc]initWithFrame:CGRectMake(30, titleLabel.origin.y+titleLabel.height+46, kScreenWidth-60, 50) type:1];
    self.phoneView.placeholder = @"请输入手机号";
    [self.view addSubview:self.phoneView];
    
    //密码
    self.pwdView = [[HNSH_LoginInputView alloc]initWithFrame:CGRectMake(30, self.phoneView.origin.y+self.phoneView.height+16, kScreenWidth-60, 50) type:0];
    self.pwdView.placeholder = @"请输入密码";
    self.pwdView.hiddenText = YES;
    [self.view addSubview:self.pwdView];
    
    //立即注册
    UIButton *registerButton = [UIButton buttonWithType:UIButtonTypeCustom];
    registerButton.frame = CGRectMake(32, self.pwdView.origin.y+self.pwdView.height+10, 60, 32);
    [registerButton setTitle:@"立即注册" forState:UIControlStateNormal];
    [registerButton setTitleColor:[UIColor appBlackLabelColor] forState:UIControlStateNormal];
    registerButton.titleLabel.font = [UIFont systemFontOfSize:14];
    [self.view addSubview:registerButton];
    
    //验证码登录
    UIButton *codeLoginButton = [UIButton buttonWithType:UIButtonTypeCustom];
    codeLoginButton.frame = CGRectMake(kScreenWidth-107, self.pwdView.origin.y+self.pwdView.height+10, 75, 32);
    [codeLoginButton setTitle:@"验证码登录" forState:UIControlStateNormal];
    [codeLoginButton setTitleColor:[UIColor appBlackLabelColor] forState:UIControlStateNormal];
    codeLoginButton.titleLabel.font = [UIFont systemFontOfSize:14];
    [self.view addSubview:codeLoginButton];
    
    //登录
    UIButton *loginButton = [UIButton buttonWithType:UIButtonTypeCustom];
    loginButton.frame = CGRectMake(30, self.pwdView.origin.y+self.pwdView.height+100, kScreenWidth-60, 50);
    [loginButton setTitle:@"登录" forState:UIControlStateNormal];
    [loginButton setTitleColor:[UIColor appWhiteLabelColor] forState:UIControlStateNormal];
    loginButton.titleLabel.font = [UIFont boldSystemFontOfSize:14];
    loginButton.backgroundColor = [UIColor appGreenColor];
    loginButton.layer.cornerRadius = 25.0f;
    loginButton.layer.masksToBounds = YES;
    [self.view addSubview:loginButton];
    
    //忘记密码
    UIButton *forgetPwdButton = [UIButton buttonWithType:UIButtonTypeCustom];
    forgetPwdButton.frame = CGRectMake(30, loginButton.origin.y+loginButton.height+10, kScreenWidth-60, 32);
    [forgetPwdButton setTitle:@"忘记密码" forState:UIControlStateNormal];
    [forgetPwdButton setTitleColor:[UIColor appBlackLabelColor] forState:UIControlStateNormal];
    forgetPwdButton.titleLabel.font = [UIFont boldSystemFontOfSize:14];
    [self.view addSubview:forgetPwdButton];
    
#pragma mark 操作区
    WeakSelf(weakSelf);
    //手机号
    self.phoneView.fieldBlock = ^(NSString * _Nonnull inputString) {
        weakSelf.phoneString = inputString;
    };
    //密码
    self.pwdView.fieldBlock = ^(NSString * _Nonnull inputString) {
        weakSelf.pwdString = inputString;
    };
    //注册
    [[registerButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        HNSH_RegisterViewController *vc = [[HNSH_RegisterViewController alloc]init];
        [weakSelf.navigationController pushViewController:vc animated:YES];
    }];
    //验证码登录
    [[codeLoginButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        HNSH_CodeLoginViewController *vc = [[HNSH_CodeLoginViewController alloc]init];
        [weakSelf.navigationController pushViewController:vc animated:YES];
    }];
    //登录
    [[loginButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        [weakSelf loadLogin];
    }];
    //忘记密码
    [[forgetPwdButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        HNSH_SafeHomeViewController *vc = [[HNSH_SafeHomeViewController alloc]init];
        [weakSelf.navigationController pushViewController:vc animated:YES];
    }];
}

#pragma mark networking
- (void)loadLogin {
    if (![self.phoneString isNotBlank]) {
        [self.view makeToast:@"请输入手机号"];
        return;
    }
    if (![self.pwdString isNotBlank]) {
        [self.view makeToast:@"请输入密码"];
        return;
    }
    [HNSH_UserNetServer requestLoginWithUserName:self.phoneString pwd:self.pwdString type:@"password" success:^(ST_BaseNetModel * _Nullable responseObject) {
        if (responseObject.code == 0) {
            //保存token
            [[ST_UserManager shareInstance] saveAccountInfoWithAccessToken:responseObject.resultset[@"access_token"] RefreshToken:responseObject.resultset[@"refresh_token"]];
            //请求个人信息
            [[ST_UserManager shareInstance] loadUserInfoComplate:^{
                //隐藏判断
                KUserShowHiddenPhoneID
                kSTWindow.rootViewController = [ST_TabbarViewController new];
//                [self.navigationController dismissViewControllerAnimated:YES completion:nil];
            }];
            
            //请求融云信息
            [[ST_UserManager shareInstance] loadRcUserInfoComplate:^{
                [HNSH_RYTools connectWithToken:[ST_UserManager shareInstance].rcUser.token];
            }];
            
//            //请求三方信息
//            [[ST_UserManager shareInstance] loadOtherTokenComplate:^{
//                            
//            }];
        }
    } Failure:^(NSError * _Nullable error) {
        
    }];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
